﻿Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports SecretaryToolData

Partial Public Class Reports

	Public Class FieldSpecs
		Public Property lowerLeftx As Single?
		Public Property lowerLefty As Single?
		Public Property Width As Single?
		Public Property Height As Single?

		Sub New(lowerLeftx As Single?, lowerLefty As Single?, Width As Single?, Height As Single?)
			Me.lowerLeftx = lowerLeftx
			Me.lowerLefty = lowerLefty
			Me.Width = Width
			Me.Height = Height
		End Sub

		Sub New()
			Me.lowerLeftx = Nothing
			Me.lowerLefty = Nothing
			Me.Width = Nothing
			Me.Height = Nothing
		End Sub
	End Class

	Private Shared BlackBaseColor As New BaseColor(Color.Black)
	Private Shared fontPageTitle As New Font(UniCodeBaseFont, 20, Font.BOLD, BlackBaseColor)
	Private Shared fontSectionLabel As New Font(UniCodeBaseFont, 14, Font.ITALIC, BlackBaseColor)
	Private Shared fontItemLabel As New Font(UniCodeBaseFont, 12, Font.BOLD, BlackBaseColor)
	Private Shared fontItem As New Font(UniCodeBaseFont, 12, Font.NORMAL, BlackBaseColor)

	Private Shared ReadOnly Property UniCodeBaseFont As BaseFont
		Get
			Dim arialUnicodePath As String = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\ARIALUNI.TTF"
			Dim myBaseFont As BaseFont = BaseFont.CreateFont(arialUnicodePath, BaseFont.IDENTITY_H, True)
			Return myBaseFont
		End Get
	End Property

	Private Shared Function PhraseChunks(ChunksToPhrase As List(Of Chunk)) As Phrase
		Dim returnPhrase As New Phrase
		returnPhrase.AddAll(ChunksToPhrase)
		Return returnPhrase
	End Function

	Private Shared Sub PlaceText(PdfContentByte As PdfContentByte, Text As String, Font As Font, lowerLeftx As Single, lowerLefty As Single, Width As Single, Height As Single, Leading As Double, Alignment As Integer, Optional DynamicSize As Boolean = True)
		lowerLefty -= ConfigPropertyKeys.S21VerticalOffset

		Dim ct As New ColumnText(PdfContentByte)

		If DynamicSize Then
			Dim textChunk As New Chunk(Text, Font)
			Dim textHeight = textChunk.Font.CalculatedSize
			Dim textWidth = textChunk.GetWidthPoint()

			'make it as big as we can first
			If Text IsNot Nothing AndAlso Text.Length > 0 Then
				Do While textHeight < Height OrElse textWidth < Width
					Font.Size = Font.Size + 0.1
					textChunk = New Chunk(Text, Font)
					textHeight = textChunk.Font.CalculatedSize
					textWidth = textChunk.GetWidthPoint()
				Loop

				'then shrink it until it just fits the space
				Do While textHeight > Height OrElse textWidth > Width
					Font.Size = Font.Size - 0.1
					textChunk = New Chunk(Text, Font)
					textHeight = textChunk.Font.CalculatedSize
					textWidth = textChunk.GetWidthPoint()
				Loop
			End If
		End If

		'leading defaults to set the line height to 1.5x the font height
		'a leading of .75 will set the line height to 1x the font height
		ct.SetSimpleColumn(New Phrase(Text, Font), lowerLeftx, lowerLefty, lowerLeftx + Width, lowerLefty + Height, Leading, Alignment)
		ct.Go()
	End Sub

	Private Shared Sub PlaceText(PdfContentByte As PdfContentByte, Text As String, Font As Font, Box As FieldSpecs, Leading As Double, Alignment As Integer, Optional DynamicSize As Boolean = True)
		PlaceText(PdfContentByte, Text, Font, Box.lowerLeftx, Box.lowerLefty, Box.Width, Box.Height, Leading, Alignment, DynamicSize)
	End Sub

	'because the vertical position and height won't change usually
	Private Shared Sub PlaceText(PdfContentByte As PdfContentByte, Text As String, Font As Font, Box As FieldSpecs, lowerLefty As Single, Height As Single, Leading As Double, Alignment As Integer, Optional DynamicSize As Boolean = True)
		PlaceText(PdfContentByte, Text, Font, Box.lowerLeftx, lowerLefty, Box.Width, Height, Leading, Alignment, DynamicSize)
	End Sub

	Private Shared Function PrettyCell(Text As String)
		Dim returnCell As New PdfPCell(New Phrase(Text))
		returnCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
		returnCell.NoWrap = False
		Return returnCell
	End Function

	Private Shared Function PrettyCell(Text As Double)
		Return PrettyCell(Text.ToString("#.##"))
	End Function

	Private Shared Function NoWrapCell(Text As String)
		Dim returnCell As New PdfPCell(New Phrase(Text))
		returnCell.NoWrap = True
		Return returnCell
	End Function
End Class

Public Class SimpleColumnText
	Inherits ColumnText

	Dim workingDocument As Document
	Dim columns As New List(Of Rectangle)
	Dim currentColumn As Integer = 0

	Public Sub New(content As PdfContentByte, columnCount As Integer, columnSpacing As Single, document As Document)
		MyBase.New(content)

		workingDocument = document
		CalculateColumnBoundries(columnCount, columnSpacing)
	End Sub

	Private Sub CalculateColumnBoundries(columnCount As Integer, columnSpacing As Single)
		Dim columnWidth As Single
		Dim columnHeight As Single

		With workingDocument
			columnWidth = ((.PageSize.Width - .LeftMargin - .RightMargin) - (columnSpacing * (columnCount - 1))) / columnCount
			columnHeight = (.PageSize.Height - .TopMargin - .BottomMargin)
		End With

		For x = 0 To columnCount - 1
			Dim llx As Single = ((columnWidth + columnSpacing) * x) + workingDocument.LeftMargin
			Dim lly As Single = workingDocument.BottomMargin
			Dim urx As Single = llx + columnWidth
			Dim ury As Single = columnHeight

			Dim newRectangle As New Rectangle(llx, lly, urx, ury)

			columns.Add(newRectangle)

		Next

	End Sub

	Public Shadows Sub AddElement(element As IElement)
		MyBase.AddElement(element)

		'we have to see if there is a column on the page before we begin
		Dim status As Integer

		If currentColumn = 0 Then
			status = ColumnText.NO_MORE_COLUMN
		End If

		Do
			If status = ColumnText.NO_MORE_COLUMN Then
				If currentColumn = columns.Count Then
					'we need a new page
					workingDocument.NewPage()
					'reset column count
					currentColumn = 0
				End If
				MyBase.SetSimpleColumn(columns(currentColumn))
				currentColumn += 1
			End If

			status = MyBase.Go()
		Loop While ColumnText.HasMoreText(status)
	End Sub
End Class